package com.cnguifang.feigewallet.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.cnguifang.feigewallet.DemoHelper;
import com.cnguifang.feigewallet.R;
import com.cnguifang.feigewallet.conference.ConferenceActivity;
import com.hyphenate.util.EasyUtils;


import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;

import java.util.Locale;

/**
 * 开屏页
 *
 */
public class SplashActivity extends BaseActivity {

	protected static final String TAG = "SplashActivity";

	private static final int sleepTime = 2000;

	private int language_id=0;

	@Override
	protected void onCreate(Bundle arg0) {
		setContentView(R.layout.em_activity_splash);
		super.onCreate(arg0);
		setLanguage();
		DemoHelper.getInstance().initHandler(this.getMainLooper());

		RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.splash_root);
		TextView versionText = (TextView) findViewById(R.id.tv_version);

		versionText.setText(getVersion());
		AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
		animation.setDuration(1500);
		rootLayout.startAnimation(animation);
	}

	private void setLanguage() {
		Resources resources=getResources();
		final SharedPreferences sharedPreferences=getSharedPreferences("language_choice", getApplicationContext()
				.MODE_PRIVATE);
		language_id = sharedPreferences.getInt("language_id", 0);
		Log.e(TAG,"language_id=="+language_id);
		// 获取应用内语言
		final Configuration configuration=resources.getConfiguration();
//        Locale locale=configuration.locale;
		DisplayMetrics displayMetrics=resources.getDisplayMetrics();
		switch (language_id){
			case 0:
				configuration.locale= Locale.getDefault();
				break;
			case 1:
				configuration.locale=Locale.ENGLISH;
				break;
			default:
				configuration.locale=Locale.SIMPLIFIED_CHINESE;
				break;
		}
		Log.e(TAG,"configuration=="+configuration.locale);
		getResources().updateConfiguration(configuration,displayMetrics);
	}

	@Override
	protected void onStart() {
		super.onStart();
		PgyUpdateManager.setIsForced(true);
		PgyUpdateManager.register(SplashActivity.this,
				new UpdateManagerListener() {

					@Override
					public void onUpdateAvailable(final String result) {

						// 将新版本信息封装到AppBean中
						final AppBean appBean = getAppBeanFromString(result);
						new AlertDialog.Builder(SplashActivity.this)
								.setTitle("更新")
								.setMessage("发现新版本，版本号："+appBean.getVersionName())
								.setPositiveButton(
										"确定",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												startDownloadTask(
														SplashActivity.this,
														appBean.getDownloadURL());
											}
										}).setNegativeButton("取消",new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface dialog,
									int which) {
								startLaunchActivity();
							}
						}).show();
					}

					@Override
					public void onNoUpdateAvailable() {
						startLaunchActivity();
					}
				});

	}
	
	/**
	 * get sdk version
	 */
	private String getVersion() {
	    return EMClient.getInstance().VERSION;
	}

	private void startLaunchActivity(){
		if (DemoHelper.getInstance().isLoggedIn()) {
			// auto login mode, make sure all group and conversation is loaed before enter the main screen
			long start = System.currentTimeMillis();
			EMClient.getInstance().chatManager().loadAllConversations();
			EMClient.getInstance().groupManager().loadAllGroups();
			String topActivityName = EasyUtils.getTopActivityName(EMClient.getInstance().getContext());
			if (topActivityName != null && (topActivityName.equals(VideoCallActivity.class.getName()) || topActivityName.equals(VoiceCallActivity.class.getName()) || topActivityName.equals(ConferenceActivity.class.getName()))) {
				// nop
				// avoid main screen overlap Calling Activity
			} else {
				//enter main screen
				startActivity(new Intent(SplashActivity.this, MainActivity.class));
			}
			finish();
		}else {
			startActivity(new Intent(SplashActivity.this, LoginActivity.class));
			finish();
		}

	}

	@Override
	protected void onStop() {
		super.onStop();
		PgyUpdateManager.unregister();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		PgyUpdateManager.unregister();
	}
}
